package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.DictionaryApi;
import com.sd365.permission.centre.dao.mapper.DictionaryMapper;
import com.sd365.permission.centre.pojo.dto.DictionaryDTO;
import com.sd365.permission.centre.pojo.query.DictionaryQuery;
import com.sd365.permission.centre.pojo.vo.DictionaryVO;
import com.sd365.permission.centre.service.DictionaryService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * @class DictionaryController
 * @classdesc
 * @author Administrator
 * @date 2020-10-8  9:58
 * @version 1.0.0
 * @see
 * @since
 */
@RestController
public class DictionaryController extends AbstractController implements DictionaryApi {
    /**
     * 客户服务类
     */
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Override
    public Boolean add(@Valid DictionaryDTO DictionaryDTO) {
        if(null == DictionaryDTO){
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID,new Exception("添加对象不允许为空"));
        }
        return dictionaryService.add(DictionaryDTO);
    }

    @Override
    public Boolean remove(@Valid @NotNull Long id, @Valid @NotNull Long version) {
        return dictionaryService.remove(id,version);
    }

    @Override
    public Boolean modify(@Valid DictionaryDTO DictionaryDTO) throws Exception {
        if(null == DictionaryDTO){
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID,new Exception("添加对象不允许为空"));
        }
        return dictionaryService.modify(DictionaryDTO);
    }

    @ApiLog
    @Override
    public Boolean batchUpdate(@Valid DictionaryDTO[] DictionaryDTOS) throws Exception {
        return dictionaryService.batchUpdate(DictionaryDTOS);
    }

    @ApiLog
    @Override
    public Boolean batchRemove(@Valid DictionaryDTO[] DictionaryDTOS) throws Exception {
        return dictionaryService.batchRemove(DictionaryDTOS);
    }

    @Override
    public DictionaryDTO copy(@Valid @NotNull Long id) {
        return null;
    }


    @ApiLog
    @Override
    public List<DictionaryVO> commonQuery(DictionaryQuery dictionaryQuery) {
        if(null==dictionaryQuery){
            throw new BusinessException(BusinessErrorCode.SYSTEM_CONTROLLER_ARGUMENT_NOT_VALID,new Exception("查询对象不允许为空"));
        }
        //执行查询
        List<DictionaryDTO> dictionaryDTOS= dictionaryService.commonQuery(dictionaryQuery);
        List<DictionaryVO> dictionaryVOS=BeanUtil.copyList(dictionaryDTOS,DictionaryVO.class);
        return dictionaryVOS;

    }

    @ApiLog
    @Override
    public DictionaryVO queryDictionaryById(@Valid @NotNull Long id) {
        //throw new BusinessException(BusinessErrorCode.SYSTEM_SERVICE_ARGUMENT_NOT_VALID,new Exception("测试异常"));
        DictionaryDTO dictionaryDTO=dictionaryService.queryById(id);
        if(dictionaryDTO!=null){
            DictionaryVO DictionaryVO=BeanUtil.copy(dictionaryDTO,DictionaryVO.class);
            return DictionaryVO;
        }else{
            return null;
        }
    }

    @Override
    public List<DictionaryVO> queryDictionaryByTypeId(@Valid @NotNull Long id) {
        List<DictionaryDTO> dictionaryDTOS=dictionaryService.queryByTypeId(id);
        if(dictionaryDTOS!=null){
            List<DictionaryVO> DictionaryVOS=BeanUtil.copyList(dictionaryDTOS,DictionaryVO.class);
            return DictionaryVOS;
        }else{
            List<DictionaryVO> DictionaryVOS=new ArrayList<>();
            return DictionaryVOS;
        }
    }
}
